﻿var itemSuggest = function (_domID, _url, _idField, _keyField, _param) {
    /**
     * 请求json格式的数据 {value: [key:value,key2:value2]};
     */

    var IS_IdField = _idField;
    var IS_keyField = _keyField;
    var dataList = { value: [] }, dataNameList = { value: [] };
    var jsonData = [];
    var domID = _domID, url = _url, param = _param, ShowBtn = false, MultiWord = false;
    this.itemPost = function () {
        $.ajax({
            type: "post",
            url: url,
            data: param,
            dataType: "json",
            success: function (datas) {
                jsonData = datas.Data;
                if (!jsonData || jsonData.length === 0) {
                    return false;
                } else {
                    var index, len;
                    len = jsonData.length;

                    for (index = 0; index < len; index++) {
                        dataList.value.push({
                            IS_IdField: jsonData[index][IS_IdField],
                            IS_keyField: jsonData[index][IS_keyField]
                        });
                        dataNameList.value.push({
                            IS_keyField: jsonData[index][IS_keyField]
                        });
                    }
                    dataList.defaults = 'wuweihua';
                }
                Init();
            }
        });
    }
    this.getSelectIds = function () {
        var sel_val = $("#" + domID).val();
        var sel_item = [];
        if (sel_val != null) {
            var sel_vals = sel_val.split(",");
            if (sel_vals == null) {
                sel_vals = sel_val.split(" ");
            }
            if (sel_vals != null) {
                for (var i = 0; i < sel_vals.length; i++) {
                    var c = sel_vals[i];
                    for (var j = 0; j < dataList.value.length; j++) {
                        var d = dataList.value[j];
                        if (c === d.IS_keyField) {
                            sel_item.push(d.IS_IdField);
                        }
                    }
                }
            }
        }
        return sel_item.join(',');
    }
    this.getSelectItem = function () {
        var item = {};
        if (jsonData != null) {
            var sel_val = $("#" + domID).val();
            for (var i = 0; i < jsonData.length; i++) {
                if (jsonData[i][IS_keyField] === sel_val) {
                    item = jsonData[i]
                }
            }
        }
        return item;
    }
    this.SetBtnHide = function (b) {
        ShowBtn = b;
    }
    this.SetMultiWord = function (b) {
        MultiWord = b;
    }
    var destroy = function () {
        $("#" + domID).bsSuggest("destroy");
    };

    var Init = function () {
        destroy();
        this.bsSuggest = $("#" + domID).bsSuggest({
            allowNoKeyword: false,   //是否允许无关键字时请求数据。为 false 则无输入时不执行过滤请求
            multiWord: MultiWord,    //以分隔符号分割的多关键字支持
            separator: ",",          //多关键字支持时的分隔符，默认为空格
            ignorecase: true,       //前端搜索匹配时，是否忽略大小写
            keyLeft: 37,                    //向左方向键
            keyUp: 38,                      //向上方向键
            keyRight: 39,                   //向右方向键
            keyDown: 40,                    //向下方向键
            keyEnter: 13,                   //回车键
            data: dataNameList,
            showBtn: ShowBtn
        });
    };
}
